package com.zhonghan.recharge.controller;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhonghan.common.bean.Result;
import com.zhonghan.common.bean.ZhCardOrder;
import com.zhonghan.common.utils.ZHApiSign;
import com.zhonghan.recharge.pojo.ZhBusinessUser;
import com.zhonghan.recharge.service.JiSuRechargeService;
import com.zhonghan.recharge.service.ZhBusinessUserService;
import com.zhonghan.recharge.service.ZhGascardOrderService;

@Controller
@RequestMapping("/zh/recharge")
public class ZhRechargeAPIController {

private static final Logger logger = LogManager.getLogger(RechargeController.class);
	
	@Autowired
	private ZhGascardOrderService zhGascardOrderService;
	
	@Autowired
	private JiSuRechargeService jiSuRechargeService;
	
	/**
	 * 
	 * @param gasnumber
	 * @param gasname
	 * @param gasphone
	 * @param gastype
	 * @param money   // 加油金额
	 * @param request
	 * @return
	 * 暂时不用
	 */
	@RequestMapping(value="/api",method=RequestMethod.POST)
	@ResponseBody
	public Result oilRechareApi(
			String appKey,
			String gasnumber, 
			String gasname, 
			String gasphone, 
			String gastype, 
			Integer money , String sign) {
		//gasnumber=1000119000002894316&gasname=靳海军&gasphone=13823116872&gastype=sinopec&money=100
		if(gasnumber == null || gasname == null || gasphone == null || gastype == null || money == null ) {
			return new Result("0", "参数不正确", null);
		}
		
		
		gasnumber.trim();
		gasname.trim();
		gasphone.trim();
		gastype.trim();
		Result result = new Result();
		if(!gasnumber.equals("") && !gasname.equals("") && !gasphone.equals("") 
				&& !gastype.equals("")) {
			
			logger.info("api..........充油开始："+gasname+" "+gasnumber+" "+gasphone+" "+gastype+" "+money);
		/**
		 * 校验
		 */
			if(ZHApiSign.check(gasnumber, gasname, gasphone, gastype, money, sign)) {
				try {
					result = jiSuRechargeService.OilRechargeServiceJiSu(gasnumber, gasname, gasphone, gastype, money);
					/**
					 * 订单生产成功 ，返回订单信息
					 */
					String orderNo = result.getData().toString();
					ZhCardOrder zhCardOrder = zhGascardOrderService.selectOrderToApiByOrderNo(orderNo);
					result.setData(zhCardOrder);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					result.setCode("0");
					result.setMsg("油卡充值失败!");
				}
				
			}else {
				result.setCode("0");
				result.setMsg("参数校验失败");
			}
			return result;
		}else {
			result.setCode("0");
			result.setMsg("参数不全");
			return result;
		}
		
		
	}
	
	
	@Autowired
	private ZhBusinessUserService zhBusinessUserService;
	
	/**
	 * 传递 订单id
	 * @param gasnumber
	 * @param gasname
	 * @param gasphone
	 * @param orderId 
	 * @param money   // 加油金额
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/api2",method=RequestMethod.POST)
	@ResponseBody
	public Result oilRechareApi2(
			String appKey,
			String gasnumber, 
			String gasname, 
			String gasphone, 
			String orderId, 
			Integer money , String sign) {
		if(gasnumber == null || gasname == null || gasphone == null || money == null ) {
			return new Result("0", "参数不正确", null);
		}
		gasnumber.trim();
		gasname.trim();
		gasphone.trim();
		Result result = new Result();
		if(!gasnumber.equals("") && !gasname.equals("") && !gasphone.equals("")) {
			
			logger.info("api2..........充油开始："+gasname+" "+gasnumber+" "+gasphone+" "+money);
			
			//huoq 
			ZhBusinessUser zhBusinessUser = zhBusinessUserService.selectZhBusinessUserByPhone(gasphone);
		/**
		 * 校验
		 */
			if(ZHApiSign.check2(zhBusinessUser.getPortAppkey(),zhBusinessUser.getPortAppsecret(),gasnumber, gasname, gasphone, orderId, money, sign)) {
				try {
					
					// 通过电话获取到该用户的 appkey appsc
					result = jiSuRechargeService.oilRechargeServiceJiSuUseOrderId(gasnumber, gasname, gasphone, orderId, money,zhBusinessUser.getPortAppkey(),zhBusinessUser.getPortAppsecret());
					/**
					 * 订单生产成功 ，返回订单信息
					 */
					String orderNo = result.getData().toString();
					ZhCardOrder zhCardOrder = zhGascardOrderService.selectOrderToApiByOrderNo(orderNo);
					result.setData(zhCardOrder);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					result.setCode("0");
					result.setMsg("油卡充值失败!");
				}
				
			}else {
				result.setCode("0");
				result.setMsg("参数校验失败");
			}
			return result;
		}else {
			result.setCode("0");
			result.setMsg("参数不全");
			return result;
		}
		
		
	}
	/**
	 * 对外api 查询订单详情
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value="/api/orderDetail", method=RequestMethod.POST)
	@ResponseBody
	public ZhCardOrder oilRechareApi(String orderNo) {
		
		/**
		 * 校验
		 */
		ZhCardOrder zhCardOrder = null;
		
		if(orderNo != null && !"".equals(orderNo)) {
			zhCardOrder = zhGascardOrderService.selectOrderToApiByOrderNo(orderNo);
		}
		if(zhCardOrder == null) {
			zhCardOrder = new ZhCardOrder();
			zhCardOrder.setRechargeState("9");
		}
		
		return zhCardOrder;
		
	}
	
}
